package com.woniuxy.Cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @auther linke
 * @create 2021-10-21 14:28
 */
@WebServlet("/cookieDemo03")
public class CookieDemo03 extends HttpServlet {
    /**
     * 设置中文作为cookie值
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      /*  //获取cookie对象
        Cookie cookie = new Cookie("name", "张三");//Set-Cookie: name=tom
       //响应添加cookie对象
        resp.addCookie(cookie);*/

        //set-cookie:name=tom 直接设置响应头
       /* resp.setCharacterEncoding("utf-8");//不能更嘎请求头的格式*/
        //String value = "张三";
        String vlaue1 = URLEncoder.encode("张三", "utf-8");//set-cookie: name=%E5%BC%A0%E4%B8%89

        resp.setHeader("set-cookie","name="+vlaue1);//set-cookie: name=??
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
